package cn.zwx.goods.svc.service.impl;

import cn.zwx.goods.svc.domain.entity.SkuAttrValueEntity;
import cn.zwx.goods.svc.domain.entity.SkuEntity;
import cn.zwx.goods.svc.domain.enums.SkuStatusEnum;
import cn.zwx.goods.svc.mapper.SkuAttrValueMapper;
import cn.zwx.goods.svc.mapper.SkuMapper;
import cn.zwx.goods.svc.service.SkuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhangwenxue
 */
@Service
public class SkuServiceImpl implements SkuService {

    @Resource
    private SkuMapper skuMapper;

    @Resource
    private SkuAttrValueMapper skuAttrValueMapper;


    @Override
    public void batchInert(List<SkuEntity> skuEntityList) {
        skuMapper.batchInsert(skuEntityList);
    }

    @Override
    public void batchInsertAttr(List<SkuAttrValueEntity> skuAttrValueList) {
        skuAttrValueMapper.batchInsert(skuAttrValueList);
    }

    @Override
    public List<SkuEntity> selectBySpuId(String spuId) {
        LambdaQueryWrapper<SkuEntity> skuWrapper = new LambdaQueryWrapper<>();
        skuWrapper.eq(SkuEntity::getSpuId,spuId);
        skuWrapper.ne(SkuEntity::getStatus, SkuStatusEnum.DELETE.getCode());
        skuWrapper.orderByDesc(SkuEntity::getSort);
        skuWrapper.orderByDesc(SkuEntity::getCreateTime);
        skuWrapper.orderByDesc(SkuEntity::getUpdateTime);
        return skuMapper.selectList(skuWrapper);
    }

    @Override
    public void deleteBySkuId(List<String> skuIdList) {
         skuMapper.deleteBySkuId(skuIdList);
    }

    @Override
    public void deleteSkuBySpuId(String spuId) {
        LambdaQueryWrapper<SkuEntity> skuWrapper = new LambdaQueryWrapper<>();
        skuWrapper.eq(SkuEntity::getSpuId,spuId);
        final List<SkuEntity> skuList = skuMapper.selectList(skuWrapper);
        if (CollectionUtils.isEmpty(skuList)){
            return;
        }
        final List<String> skuIdList = skuList.stream().map(SkuEntity::getId).collect(Collectors.toList());
        this.deleteBySkuId(skuIdList);
    }
}
